package com.niub.common.xml.parse;

import com.niub.common.xml.exception.XMLParseException;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;

import java.io.StringReader;

/**
 * @author : acfan
 * date : create in 2023/7/20 10:06
 * description :
 **/

public class XMLParse {
    public static <T> T xml2Bean(String xml, Class<T> clazz) throws XMLParseException {
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            return clazz.cast(unmarshaller.unmarshal(new StringReader(xml))) ;
        } catch (JAXBException e) {
            throw new XMLParseException(e.getMessage());
        }
    }
}
